package com.zs.zcerp.approval.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.zcerp.approval.entity.QhbillstdmEntity;
import com.zs.zcerp.approval.mapper.QhbillstdmMapper;
import com.zs.zcerp.approval.service.IQhbillstdmService;
import com.zs.zcerp.common.entity.FebsConstant;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * Henry
 */

@Service
@RequiredArgsConstructor
@DS("oracle") //从oracle库数据查询
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class QhbillstdmServiceImpl extends ServiceImpl<QhbillstdmMapper, QhbillstdmEntity> implements IQhbillstdmService {

    @Override
    public IPage<QhbillstdmEntity> findClassifyDetailList(QhbillstdmEntity qhbillstdmEntity, QueryRequest request) {
        Page<QhbillstdmEntity> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countClassifyDetail(qhbillstdmEntity));
        SortUtil.handlePageSort(request, page, "billstdNo", FebsConstant.ORDER_ASC, false);
        return baseMapper.findClassifyDetailPage(page, qhbillstdmEntity);
    }

    @Override
    public QhbillstdmEntity findClassifyDetailList(String billstdNo) {
        QhbillstdmEntity param = new QhbillstdmEntity();
        param.setBillstdNo(billstdNo);
        List<QhbillstdmEntity>  classifys = baseMapper.findClassifyDetail(param);
        return CollectionUtils.isNotEmpty(classifys) ? classifys.get(0) : null;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBase(QhbillstdmEntity qhbillstdmEntity) {
        save(qhbillstdmEntity);

    }

    @Override
    public void updateBase(QhbillstdmEntity qhbillstdmEntity) {
       baseMapper.update(qhbillstdmEntity, new LambdaQueryWrapper<QhbillstdmEntity>().eq(QhbillstdmEntity::getBillstdNo, qhbillstdmEntity.getBillstdNo()));

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBaseByBasestdNo(String[] billstdNos) {
        List<String> list = Arrays.asList(billstdNos);
        baseMapper.delete(new QueryWrapper<QhbillstdmEntity>().lambda().in(QhbillstdmEntity::getBillstdNo, list));
    }
    

    @Override
    public QhbillstdmEntity findByBasestdNo(String billstdNo) {
        return baseMapper.findByBasestdNo(billstdNo);
    }



}
